/****************************************************************************************
 *
 * ļ: main.c
 * Ŀ:
 *  : v1.0
 *  : 20151209 18ʱ5506
 *  : Administrator
 * 
 ****************************************************************************************/
#include "main.h"


unsigned char kf8_touch_flag;
unsigned char kf8_time_flag;	// ʱ־
unsigned char fast_key_flag;	// ʱ־
unsigned int  Touch_Watch_Count;		// شУʱ޹Ϊʶ³ʼ
unsigned int clr_show_watch=0;
unsigned char clr_show_enable=1;
unsigned int  key_time_count;
unsigned int  fast_Count;
unsigned long  _KF8_LIBi_channel_flag_Old_;	// һΰ־ʵְ仯ִа
unsigned char key_out;
unsigned int  time;				// ʱ
unsigned char PWM3_T;
unsigned int  temper_int;				// ʱ
unsigned char PWM1L_back;
signed	int	PWM1L_int;
unsigned char PWM2L_back;
signed	int	PWM2L_int;
unsigned char PWM3;
unsigned char PWM3L_back;

unsigned int wheel_temp_data;
#define LED_G_ON	P0LR3=1;
#define LED_G_OFF	P0LR3=0;

//unsigned	int	_KF8_LIBi_Slide_channel_out0_;
//unsigned	int	_KF8_LIBi_Slide_channel_out1_;
//;************************************************************************************
//;*   :  delay_ms
//;* : ʱ
//;* ڲ: 
//;*     :   
//;************************************************************************************
void delay_ms(uint ms_data)
{
	uint j=0;
	while(ms_data--)
	{
		_CWDT();
		j=100;///200;
		while(j--);
	}
}
//;************************************************************************************
//;*   :  delay_us
//;* : ʱ
//;* ڲ: 
//;*     :   
//;************************************************************************************
void delay_us(uint us_data)
{
	while(us_data--);
}
//;************************************************************************************
//;*   :
//;* : Ϣ
//;* ڲ: 
//;*     :   
//;************************************************************************************
void Show_Key_Out()
{
	if((_KF8_LIBi_Slide_Press_Flag&0x03)==0)
	{
		PWM1L_back=PWM1L;
		PWM2L_back=PWM2L;
	}
	if(kf8_time_flag) // ʱ鿴ֽʾ̫죬ʱ϶࣬Ӱ촥ٶ
	{
		kf8_time_flag=0;
	//((((((((((((((((((((((((((((((((((((((((
		if(_KF8_LIBi_Slide_Press_Flag&0x01)
		{
			temper_int=_KF8_LIBi_Slide_distance_out_[0];
			PWM2L_int=temper_int;

			PWM2L_int=PWM2L_int+PWM2L_back;
			if(PWM2L_int>255)
			{
				PWM2L=255;
			}
			else if(PWM2L_int<0)
			{
				PWM2L=0;
			}
			else
			{
				PWM2L=PWM2L_int;
			}
			Show_Slide_work_out();
			clr_show_watch=0;
		}
		if(_KF8_LIBi_Slide_Press_Flag&0x02)
		{
			temper_int=_KF8_LIBi_Slide_distance_out_[1];
			PWM1L_int=temper_int;
			if(temper_int > wheel_temp_data)
			{
				if(temper_int>wheel_temp_data+1)
				{
					wheel_temp_data = temper_int;
				}
			}
			else if(temper_int < wheel_temp_data)
			{
				if(wheel_temp_data>temper_int+1)
				{
					wheel_temp_data = temper_int;
				}
			}
			_KF8_LIBi_Slide_channel_out_[1] = wheel_temp_data;
			PWM1L_int=PWM1L_int+PWM1L_back;
			if(PWM1L_int>255)
			{
				PWM1L=255;
			}
			else if(PWM1L_int<0)
			{
				PWM1L=0;
			}
			else
			{
				PWM1L=PWM1L_int;
			}
			Show_Wheel_work_out();
			clr_show_watch=0;
		}
		if(_KF8_LIBi_channel_flag_Old_!=_KF8_LIBi_channel_flag_||fast_key_flag)
		{
			#define Key_1_Code_Define	0x000010  //CT 4	+
			#define Key_2_Code_Define	0x000008  //CT 3    -

			fast_key_flag=0;
			_KF8_LIBi_channel_flag_Old_=_KF8_LIBi_channel_flag_;

			if(_KF8_LIBi_channel_flag_Old_==Key_1_Code_Define)
			{
				if(PWM3!=51)
				{
					PWM3=PWM3+1;
				}
				PWM3L_back=PWM3*5;
				Show_Key1_work_out();
				clr_show_enable=0;
			}
			else if(_KF8_LIBi_channel_flag_Old_==Key_2_Code_Define)
			{
				if(PWM3!=0)
				{
					PWM3=PWM3-1;
				}
				PWM3L_back=PWM3*5;
				Show_Key2_work_out();
				clr_show_enable=0;
			}
			else								// 
			{
				key_out=0;
				key_time_count=0;
				clr_show_enable=1;
				Show_begin_Mes();
			}
			//clr_show_watch=0;
		}
		//----------------------
		clr_show_watch++;
		if(clr_show_watch==8)
		{
			clr_show_watch=16;

			if(clr_show_enable)
				Show_begin_Mes();
		}
		//))))))))))))))))))))))))))))))))))))))))))))))))
	}
}
//;************************************************************************************
//;*   :  void main()
//;* : ں
//;* ڲ: 
//;*     :   
//;************************************************************************************
void main()
{
	init_mcu();						//ʼMCUĴ
	delay_ms(200);

	#if	LCD_PRINT
		PP1=0XFF;
		PP2=0XFF;
		PWM1L=0X00;
		PWM2L=0X00;
		PWM1ON=1;
		PWM2ON=1;
		T1ON=1;
		PWM3=0;
		PWM3L_back=0;

		LCD1602_Init();
		Show_begin_Mes();
	#endif

#if	use_vdd_ref
	CTCTL1 = 0x41;//             //ƵԴ    0/4/8/C & 1
	VDAC=0x90;// 0.5VDD       ʽ0.05VddN+1)    N=0--15
#else
	CTCTL1 	= 0x40;		//ѹԴʱӷƵ  0 4 8 C~ 4/8/16/32  xxyy yyyx   0~ Vref  1~ VDD
	// ѹѡʹ
	VRECTL =0x92;
	__asm
	;;//VDACS=0x40; 		// ĴתѹҲѡVref	0x40 Vref  0x00  Vdd, ĬVdd
		MOVB #0x00
		MOV R0,#0x40
		ORL 0x1A,R0
	__endasm;
	VDAC=0x90;// 0.5Vref       ʽ0.05VxxN+1)    N=0--15
#endif
	kf8_touch_flag=0;
	_KF8_LIBi_channel_flag_Old_=0x000000;

	_KF8TS27_Lib_Set_Slide_W_Parameter(0);// kf8ts27_touch_s.h˵ʹT4ɲ
	_KF8TS27_Init_Touch_Slide_W_();		//õݴ⺯ĳʼ
//;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
	while(1)
	{
		_CWDT();
//----------------------------------------------------
		if(kf8_touch_flag)
		{
			kf8_touch_flag=0;
			//ԺTOUCH_DEBUG_TRS_DEAL ʵֲֵͲοֵ䵽λУ˵
			TOUCH_DEBUG_TRS_DEAL();
			//ϵͳҪߣӦڸúǰѺԸݴ򵥴ߡ
			_KF8TS27_Touch_Process_Slide_W_();
			//----------------------------------
		#if	LCD_PRINT
			Show_Key_Out();
		#endif
		}
		//@@@@@@@=====================================================@@@@//
		//@@@@@@@============== ===========================@@@@//
		//@@@@@@@=====================================================@@@@//
		#if 0	// Ƕʡռ
			Touch_Watch_Count++;
			if(Touch_Watch_Count>=50000) // ϸʱ  50000=0xC350
			{
				Touch_Watch_Count=0;
				_KF8TS27_Init_Touch_Slide_W_();
			}
		#else
			__asm
				BANKSEL _Touch_Watch_Count
				INC _Touch_Watch_Count
				JB PSW,2
				INC _Touch_Watch_Count+1	// ++
				MOV R0,_Touch_Watch_Count+1
				MOV R1,#0xC3
				SUB R1,R0 	;//λ
			    JB PSW,0
			    JMP Watch_Touch_Deal_End
			    JB PSW,2	;//жϵλ򲻽λһ
			    JMP Watch_Touch_Deal_DO
			    MOV R0,_Touch_Watch_Count
			    MOV R1,#0x50
			    SUB R1,R0	;//λ
			    JB PSW,0
			    JMP Watch_Touch_Deal_End
			Watch_Touch_Deal_DO:
				BANKSEL _Touch_Watch_Count
				CLR _Touch_Watch_Count
				CLR _Touch_Watch_Count+1
				PAGESEL __KF8TS27_Init_Touch_Slide_W_
				CALL __KF8TS27_Init_Touch_Slide_W_
				PAGESEL $
			Watch_Touch_Deal_End:
			__endasm;
			if(1);
		#endif
		//@@@@@@@=====================================================@@@@//
		//@@@@@@@============== =======================@@@@//
		//@@@@@@@=====================================================@@@@//


		//ԺTOUCH_DEBUG_SEND_OUT ɴ4BYTE
		//ʽ AA 05  XX XX  XX  XX  SUM  BB, 05Ϊ ʶã05ȷԤʾŴ·ͨ
		// ҪʹôDTRѡĴڹߣ115200Ҫֶܣ˵
		//			sentbuf1=0x01;
		//			sentbuf2=0x01;
		//			sentbuf3=0x01;
		//			sentbuf4=0x01;
		//			TOUCH_DEBUG_SEND_OUT();
//----------------------------------------------------
	}
}
//жϺ0:0X04ڵַ
void int_fun0() __interrupt (0)
{
//;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
#if	touch_use_T4
	if(T3IF)
	{
		T3IF=0;		//־λ־ִ
		T4CTL=0x00;
		kf8_touch_flag=1;
		Touch_Watch_Count=0;
	}
#endif
//;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

//;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
}
//жϺ1:0x14ڵַ
void int_fun1() __interrupt (1)
{
#if !touch_use_T4		// ʹT4ʱʱT3жϿʹõȼ
	if(T3IF)
	{
		T3IF=0;		//Ϊ˲Ӱжϣ԰Ѵͨŵڴһ־λȻ־λɨ輴
		kf8_touch_flag=1;	//־λ
		Touch_Watch_Count=0;
	}
#endif

	if(T0IF)
	{
		T0IF=0;
		T0=0x20;//125us
#if	LCD_PRINT
		time++;
		if(time>800)
		{
			time=0;
			kf8_time_flag=1;	// 100ms
	    }

		PWM3_T++;
		if(PWM3_T>50)
		{
			PWM3_T=0;
		}
		if(PWM3_T<PWM3)
		{
			LED_G_ON
		}
		else
		{
			LED_G_OFF
		}
		if(clr_show_enable==0)
		{
			key_time_count++;
			if(key_time_count>24000)
			{
				key_time_count--;
				fast_Count++;
				if(fast_Count>1600)
				{
					fast_Count=0;
					fast_key_flag=1;
				}
			}
			else if(key_time_count>16000)
			{
				fast_Count++;
				if(fast_Count>2400)
				{
					fast_Count=0;
					fast_key_flag=1;
				}
			}
			else if(key_time_count>8000)
			{
				fast_Count++;
				if(fast_Count>3200)
				{
					fast_Count=0;
					fast_key_flag=1;
				}
			}
			else
			{
				fast_Count=0;
			}
		}
#endif
	}
//;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
}
